<template>
	<eui-page-frame topHeight="180" bottomHeight="96">
		<template #top>
			<view class="content-img">
				<eui-navbar ref="EuiNavBar" :title="c_id" color="#000000" backgroundColor="#FFFFFF">
				</eui-navbar>
			</view>
		</template>
		<eui-page-content>
			<view>
				<eui-card title="单头信息" h-padding="28" @click="openPopupMain">
					<template v-slot:extra>
						<view class="extra-view" @click="openPopupMain">
							<eui-uicons size="32" fontFamily="enjoyIcon5"
								customPrefix="icon-jiantouxiangyou" />
						</view>
					</template>
					<eui-detail-show :items="indexItems" v-model="origData.main" size="small"></eui-detail-show>
				</eui-card>
				<view style="margin-top: 20rpx;">
					<eui-card :title="index + 1 + '. [' + item.c_gcode + ']' + item.c_gname" h-padding="28"
						v-for="(item, index) in origData.details" :key="index" @click="openPopupGoodsInfo(item)">
						<template v-slot:extra>
							<view class="extra-view" @click="openPopupGoodsInfo(item)">
								<eui-uicons size="32" fontFamily="enjoyIcon5"
									customPrefix="icon-jiantouxiangyou" />
							</view>
						</template>
						<eui-detail-show :items="dataGoodsItems" v-model="origData.details[index]"
							size="small"></eui-detail-show>
					</eui-card>
				</view>
			</view>

		</eui-page-content>
		<template #bottom>
			<view class="bt-button">
				<eui-button type="primary" title="审核" @buttonClick="auditClick"
					:disabled="!['0'].includes(origData.main.c_status) || origData.main.c_in_store_id !=  sUserStore || origData.main.c_summary == '0'" />
			</view>
		</template>
	</eui-page-frame>
	
	<eui-popup ref="popupMain" background-color="#f8f8f8" type="bottom" :mask-click="false">
		<view class="popup-view">
			<view class="popup-title">基本信息</view>
			<scroll-view class="popup-content" scroll-y>
				<eui-card-input class="popup-field" v-model="currentMain.c_out_store_id" title="发货机构"
					type="textname" :name="getRelatedName('rdsStores', currentMain.c_out_store_id)||''"
					displayable="true" placeholder=""/>
				<eui-card-input class="popup-field" v-model="currentMain.c_out_wno" title="发货仓库" type="textname"
					:name="getRelatedName('rdsWnos', currentMain.c_out_wno)||''" placeholder=""/>
				<eui-card-input v-model="currentMain.c_end_dt" type="text" title="截止日期" displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_summary" type="textname"
					:name='sysConfig.c_summary[currentMain.c_summary]' title="完成情况" displayable="true" />
				<eui-card-input v-model="currentMain.c_sku_out" type="number" title="发货品种数"
					displayable="true" />
				<eui-card-input v-model="currentMain.c_n_out" type="number" title="发货总数量" displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_at_pay_out" type="number" title="发货结算总额"
					displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_out_au_dt" title="发货审核时间" displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_out_au_userno"
					:name="getRelatedName('rdsUsers', currentMain.c_out_au_userno)||''" type="textname"
					title="发货审核人" displayable="true" placeholder=""/>
				<eui-card-input class="popup-field" v-model="currentMain.c_in_store_id" title="配送中心"
					type="textname" :name="getRelatedName('rdsStores', currentMain.c_in_store_id)||''"
					displayable="true" placeholder=""/>
				<eui-card-input class="popup-field" v-model="currentMain.c_in_wno" title="退配仓库" type="textname"
					:name="getRelatedName('rdsWnos', currentMain.c_in_wno)||''" displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_sku_in" type="number" title="收货品种数" displayable placeholder=""/>
				<eui-card-input v-model="currentMain.c_n_in" type="number" title="收货总数量" displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_at_pay_in" type="number" title="收货结算总额"
					displayable="true" placeholder="" />
				<eui-card-input v-model="currentMain.c_in_au_dt" title="收货审核时间" displayable  placeholder=""/>
				<eui-card-input v-model="currentMain.c_in_au_userno"
					:name="getRelatedName('rdsUsers', currentMain.c_in_au_userno)||''" type="textname"
					title="收货审核人" displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_note" title="备注" displayable="true" placeholder=""/>
				<eui-card-input v-model="currentMain.c_reason" title="原因" displayable="true" placeholder=""/>
	
			</scroll-view>
			<view class="bt-button">
				<eui-button type="max" title="关闭" @buttonClick="cancelMainClick" />
			</view>
		</view>
	</eui-popup>
	
	<eui-popup ref="popupGoodsInfo" background-color="#f8f8f8" type="bottom" :mask-click="false">
		<view class="popup-view">
			<view class="popup-title">商品信息</view>
			<scroll-view class="popup-content" scroll-y>
				<eui-card-input v-model="currentGds.c_name" title="商品名称" placeholder="" displayable />
				<eui-card-input v-model="currentGds.c_gcode" title="商品编码" placeholder="" displayable />
				<eui-card-input v-model="currentGds.c_barcode" title="商品条码" placeholder="" displayable />
				<eui-card-input v-model="currentGds.c_serial_no" title="批号" placeholder="" displayable />
				<eui-card-input v-model="currentGds.c_move_pack" title="包装" placeholder="" displayable />
				<eui-card-input v-model="currentGds.c_move_unit" title="单位" placeholder="" displayable />
				<eui-card-input v-model="currentGds.c_pt_type" title="执行价格类型" :name="sysConfig.c_pt_type[currentGds.c_pt_type]" type="textname"
					 displayable placeholder="" />
				<eui-card-input v-model="currentGds.c_pt_move_fact" title="退配价" type="number" placeholder=""
					displayable />
				<eui-card-input v-if="sysConfig.inputType === '1'" v-model="currentGds.c_n_pack_out" type="number" title="发货件数" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-if="sysConfig.inputType === '1'" v-model="currentGds.c_n_odd_out" type="digit" title="发货零数" maxlength="9" placeholder=""
					displayable />
        <eui-card-input v-if="sysConfig.inputType === '2'" v-model="currentGds.c_n_input_out" type="number" title="发货包装数" maxlength="9" placeholder=""
                        displayable />
				<eui-card-input v-model="currentGds.c_n_out" type="number" title="发货数量" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-model="currentGds.c_n_free_out" type="number" title="发货赠品数量" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-model="currentGds.c_at_pay_out" type="number" title="发货退配金额" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-if="sysConfig.inputType === '1'" v-model="currentGds.c_n_pack_in" type="number" title="收货件数" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-if="sysConfig.inputType === '1'" v-model="currentGds.c_n_odd_in" type="number" title="收货零数" maxlength="9" placeholder=""
					displayable />
        <eui-card-input v-if="sysConfig.inputType === '2'" v-model="currentGds.c_n_input_in" type="number" title="收货包装数" maxlength="9" placeholder=""
                        displayable />
				<eui-card-input v-model="currentGds.c_n_in" type="number" title="收货数量" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-model="currentGds.c_n_free_in" type="number" title="收货赠品数量" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-model="currentGds.c_at_pay_in" type="number" title="收货配送金额" maxlength="9" placeholder=""
					displayable />
				<eui-card-input v-model="currentGds.c_reason" title="拒收/退货原因" displayable placeholder=""/>
				<eui-card-input v-model="currentGds.c_note" title="备注" displayable placeholder=""/>
			</scroll-view>
			<view class="bt-button">
				<eui-button type="max" title="关闭" @buttonClick="cancelGdsClick" />
			</view>
		</view>
	</eui-popup>
			
</template>

<script setup type="ts">
	import { ref, reactive, onMounted } from 'vue';
	import {useMainStore} from "@/store/modules/store";
	import {useLoginStore} from "@/store/modules/useLoginStore";
	import { validateStores } from '@/hooks/useValidateMethod';
	import dateRangUtil from '../../../common/dateRangUtil';
	import { getRelatedName, getDictName,getDictSelectList, getDictSelect,getDictList } from '../components/RelatedName.js';
	import Enjoy from "@/common/enjoy";
	import {onLoad} from "@dcloudio/uni-app";
  import {getRdsSysConfig, init} from '@/hooks/init';
	import useService from '@/hooks/useService';
	import dayjs from 'dayjs';
	const service = useService({ prefix: "scApi" });
	
	const mainStore = useMainStore();
	const LoginStore = useLoginStore();
	const { loginInfo } = mainStore;
	const EuiNavBar = ref(null);
	const popupMain = ref(null);
	const popupGoodsInfo = ref(null);
	const sUserStore = ref(''); // 用户默认机构编码
	const sUserStoreName = ref(''); // 用户默认机构名称
	const sUser = ref(''); // 当前用户编码
	const c_id = ref(''); // 标题栏显示单号
	const currentMain = ref({}); // 新增/修改主表信息
	const currentGds = ref({}); // 新增/修改商品信息
	const dataList = ref([]); // 商品缓存信息
	const origData = ref({
	                main: {   
						     
	                }, // 主表
	                details: [
	                ], // 商品明显
	            }) // 盘点单明显
	            // 系统参数，字典
  const sysConfig = ref({
    c_summary: {}, // 完成情况总结： 0-未收货、1-收货中、2-部分完成、3-圆满完成
    c_status: {}, // IO类单据状态：0-尚未审核、2、预审核、3-已审核、7-已终止、8-已完成、9-已作废
    c_pt_type: {}, // 执行进价类型：C-合同价、P-促销价、D-折扣价、S-指定价、B-批次价
    inputType: '1' // 1-件数零数分别录入方式 2-自动识别含量单值录入方式
  })
	            // 下拉选择字典
	const selectRange = ref({
	                c_is_virtual: [], // 是否虚单
	                c_pt_type: [], // 执行价格类型
	            });
	            // 扫码信息
	const indexItems = ref([
		{
			label: "发货机构",
			prop: "c_out_store_name",
			type: "text",
			colSpan: 24,
			element: {
			},
		},
		{
			label: "配送中心",
			prop: "c_in_store_name",
			type: "text",
			colSpan: 12,
			element: {
			},
		},
		{
			label: "发货仓库",
			prop: "c_out_wno_name",
			type: "text",
			colSpan: 12,
			element: {
				precision: 0
			},
		},
		{
			label: "退配仓库",
			prop: "c_in_wno_name",
			type: "text",
			colSpan: 12,
			element: {
				precision: 2
			},
		},
		{
			label: "发货品种数",
			prop: "c_sku_out",
			type: "text",
			colSpan: 12,
			element: {
				precision: 2
			},
		},
		{
			label: "收货品种数",
			prop: "c_sku_in",
			type: "text",
			colSpan: 12,
			element:{
				precision: 2
			},
		},
		{
			label: "发货总数量",
			prop: "c_n_out",
			type: "text",
			colSpan: 12,
			element: {
				precision: 0
			},
		},
		{
			label: "收货总数量",
			prop: "c_n_in",
			type: "text",
			colSpan: 12,
			element: {
				precision: 2
			},
		},
		{
			label: "收货数量",
			prop: "c_n_in",
			type: "text",
			colSpan: 12,
			element: {
				precision: 0
			},
		},
		{
			label: "发货结算总额",
			prop: "c_at_pay_out",
			type: "text",
			colSpan: 12,
			element: {
				precision: 0
			},
		},
		{
			label: "完成情况",
			prop: "c_summary_name",
			type: "text",
			colSpan: 12,
			element: {
			},
		},
		{
			label: "截止日期",
			prop: "c_end_dt",
			type: "text",
			colSpan: 24,
			element: {
				
			},
		},
		{
			label: "录入",
			prop: "c_mk_userinfo",
			type: "text",
			colSpan: 24,
			element: {
				
			},
		},
		{
			label: "审核",
			prop: "c_out_au_info",
			type: "text",
			colSpan: 24,
			element: {
			},
		},
	])
	const dataGoodsItems = ref([
		{
			label: "商品(编码/条码)",
			prop: "c_gcode_barcode",
			type: "text",
			colSpan: 24,
			element: {
			},
		},
		{
			label: "包装/单位",
			prop: "c_move_pack_unit",
			type: "text",
			colSpan: 24,
			element: {
			},
		},
		{
			label: "退配价",
			prop: "c_pt_move_fact",
			type: "text",
			colSpan: 12,
			element: {
				precision: 2
			},
		},
		{
			label: "收货数量",
			prop: "c_n_in",
			type: "text",
			colSpan: 12,
			element: {
				precision: 2
			},
		},
	])
	
	onLoad(async ({id, c_gcode, c_store_id}) => {
	  await getSysConfig();
	  c_id.value = id;
	  sUserStore.value = c_store_id
	  await getData(id);
	})
	// 加载字典
	const getSysConfig = async () => {
		const orderList = await getDictList("订单完成情况总结");
		const psSatusList = await getDictList("配送价类型");
		const ioSatusList = await getDictList("IO类单据状态");
		orderList.forEach(item => sysConfig.value.c_summary[item.c_id] = item.c_name);
		psSatusList.forEach(item => {
                sysConfig.value.c_pt_type[item.c_id] = item.c_name;
                selectRange.value.c_pt_type.push({
                    text: item.c_name,
                    value: item.c_id,
                    checked: item.value == '0',
                });
        });
		// service.addlocalStorage('rdsOrderSummary', this.sysConfig.c_summary);
		// service.addlocalStorage('rdsPtTypes', this.sysConfig.c_pt_type);
		ioSatusList.forEach(e => {
			sysConfig.value.c_status[e.c_id] = e.c_name;
		});
    sysConfig.value.inputType = await getRdsSysConfig("供应链管理参数", "商品件数零数录入方式") || '1';

  }
	
	const getData = async(id)=>{
		if (id == '') return;
		// 根据单据id获取明细
		const objectData = {
			BillId: id,
		};
		uni.showLoading({ title: "正在加载...", mask: true })
		const result = await service.request('商品配送单', 'EnjoyBillQuery', objectData);
		uni.hideLoading(); 
		console.log('resultresultresult',result)
		origData.value.main = result.ds[0];
		origData.value.main.c_out_store_name =`[${origData.value.main?.c_out_store_id}]${getRelatedName('rdsStores', origData.value.main?.c_out_store_id)||''}`; 
		origData.value.main.c_in_store_name = `[${origData.value.main?.c_in_store_id}]${getRelatedName('rdsStores', origData.value.main?.c_in_store_id)||''}`; 
		origData.value.main.c_out_wno_name = `[${origData.value.main?.c_out_wno}]${getRelatedName('rdsWnos', origData.value.main?.c_out_wno)||''}`;
		origData.value.main.c_in_wno_name =`[${origData.value.main?.c_in_wno}]${getRelatedName('rdsWnos', origData.value.main?.c_in_wno)||''}`;
		origData.value.main.c_summary_name = sysConfig.value.c_summary[origData.value.main.c_summary];
		origData.value.main.c_end_dt = origData.value.main.c_end_dt ? dayjs(origData.value.main.c_end_dt).format('YYYY-MM-DD HH:mm:ss'):'';
		origData.value.main.c_mk_username = `[${origData.value.main?.c_mk_userno}]${getRelatedName('rdsUsers', origData.value.main?.c_mk_userno)||''}`;
		origData.value.main.c_mk_dt = dayjs(origData.value.main.c_mk_dt).format('YYYY-MM-DD HH:mm:ss');
		origData.value.main.c_out_au_username = origData.value.main?.c_out_au_userno ? `[${origData.value.main?.c_out_au_userno}]${getRelatedName('rdsUsers', origData.value.main?.c_out_au_userno)||''}`:'';
		origData.value.main.c_out_au_dt = origData.value.main.c_out_au_dt ? dayjs(origData.value.main.c_out_au_dt).format('YYYY-MM-DD HH:mm:ss'):'';
		origData.value.main.c_mk_userinfo = origData.value.main.c_mk_username + '  '+origData.value.main.c_mk_dt;
		origData.value.main.c_out_au_info = origData.value.main.c_out_au_username +'  '+origData.value.main.c_out_au_dt;
		origData.value.details = result.ds1.map(x=>{
			return {
				...x,
				c_gcode_barcode:x.c_gcode+'/'+x.c_barcode,
				c_move_pack_unit:x.c_move_pack.toFixed(2)+'/'+x.c_move_unit,
				c_pt_move_fact:x.c_pt_move_fact.toFixed(4),
				c_n_in:x.c_n_in.toFixed(2)
			}
		});
		c_id.value = id;
		currentMain.value = {
			...result.ds[0]
		}
	}
	
	const auditClick = async()=>{
		const BillId = c_id.value;
		const objectData = {
		                BillId: BillId,
		                UserNo: sUser.value,
		            };
		  uni.showModal({
		    title: '提示',
		    content: `请确认，审核单据[${BillId}]吗?`,
		    success: async function (res) {
		      if (res.confirm) {
		        uni.showLoading({ title: "正在加载...", mask: true })
				const result = await service.request('商品配送单', 'EnjoyBillAudit', objectData,'811');
				uni.hideLoading(); 
				uni.showToast({
		          duration: 3500,
		          icon: 'none',
		          title: `审核成功！`,
		        });
		        await getData();
		      } else if (res.cancel) {
		      }
		    },
		  })
	}
	
	// 修改
    const openPopupMain = (e) =>{
		//if (origData.value.main?.c_status != 0) return;
		popupMain.value.open('bottom');
	}
	const cancelMainClick = ()=>{
		popupMain.value.close('bottom');
	}
	// 修改商品明细
    const  openPopupGoodsInfo = (e) => {
		//if (origData.value.main?.c_status != 0) return;
		currentGds.value = {
			...e,
			c_name: e.c_gname,
			c_unit: e.c_req_unit,
			c_content: e.c_req_pack,
			c_g_adno: e.c_adno,		
		};
		c_id.value = e.c_id;
		popupGoodsInfo.value.open('bottom');
	}
	
	const cancelGdsClick = ()=>{
		popupGoodsInfo.value.close();
	}
</script>

<style lang="scss" scoped>
	@import '../../../main.scss';
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		background-color: #F2F2F2;
		padding: 0px !important
	}

	.content-img {
		background-color: #FFFFFF;
		background-size: 100% 176rpx;
		background-repeat: no-repeat;
		width: 100%;
	}

	.navigation {
		flex: 0 0 auto;
		margin-bottom: 5px;
	}

	.bt-button {
		background-color: #FFFFFF;
		padding: 10rpx 0rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	.popup-title {
		width: 100%;
		height: 84rpx;
		background: #FFFFFF;
		border-radius: 40rpx 40rpx 0 0;
		display: flex;
		justify-content: center;
		align-items: center;
		color: $uni-text-color-secondary;
		font-size: $uni-font-size-level1;
		line-height: 45rpx;
		text-align: center;
		position: relative;
	}

	// 弹框样式
	.popup-view {
		display: flex;
		flex-direction: column;
		width: 100%;
		border-radius: 20rpx 20rpx 0 0;
		background-color: #ffffff;

		.popup-content {
			display: flex;
			flex-direction: column;
			padding: 0;
			margin: 0;
			width: 100%;
			max-height: 480px;
		}

		/* 当屏幕高度小于800px时 */
		@media screen and (max-height: 800px) {
			.popup-content {
				max-height: 300px;
			}
		}

		.popup-bottom {
			background: #ffffff;
			//margin: 5px 0px 0px;
			padding: 5px 0;
			//border-top: 1px solid #cccccc;
		}

	}
	::v-deep .white-main{
		left:0;
		right:0;
	}
</style>